package ru.ifmo.croak.sample;

import ru.ifmo.croak.api.*;
import ru.ifmo.croak.util.CroakFactory;

import java.util.logging.Logger;

/**
 * Sample implementation of Query UI component that does not have any UI, but just queries sample messages.
 *
 * @author Roman Elizarov
 */
public class QueryUI implements Runnable {
    private static final Logger log = Logger.getLogger(QueryUI.class.getName());

    public static void main(String[] args) {
        new QueryUI("ui.query").run();
    }

    private final String name;

    public QueryUI(String name) {
        this.name = name;
    }

    public void run() {
        String queryProviderName = CroakFactory.getInstance().getProperty(name + ".provider");
        QueryService query = CroakFactory.getInstance().getImplementation(QueryService.class, queryProviderName);
        QueryRequest request = new QueryRequest();
        request.setAuthor("sample");
        request.setLimit(10);
        MessageQuery messageQuery = query.createQuery(request);
        messageQuery.setMessageListener(new SampleMessageListener());
    }

    private static class SampleMessageListener implements MessageListener {
        public void messageAvailable(Message message) {
            log.info("Message available " + message);
        }
    }
}
